package com.atguigu.educenter.controller;


import com.aiguigu.common_utils.JwtUtils;
import com.aiguigu.common_utils.R;
import com.aiguigu.common_utils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zzc
 * @since 2022-10-09
 */
//@CrossOrigin //跨域
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //业务层的登录方法login返回一个token值
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }
    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo register){
        memberService.register(register);
        return R.ok();
    }
    //根据token字符获得会员id
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用Jwt工具类方法，根据request对象获取头信息，返回用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberIdByJwtToken);
        return R.ok().data("userInfo",member);
    }
    //根据用户id获取用户信息(该方法供远程调用使用)
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面的值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);

        return ucenterMemberOrder;
    }
    //查询某天注册人数(该方法供远程调用使用)
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister", count);
    }
}

